﻿using System;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Services;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class HomePageBlog : BaseNopFrontendUserControl
    {
        private IBlogService _blogService;

        public HomePageBlog() {
            _blogService = IoC.Resolve<IBlogService>();
        }

        protected override void OnInit(EventArgs e) {          
            Load += new EventHandler(HomePageBlog_Load);

            base.OnInit(e);
        }

        void HomePageBlog_Load(object sender, EventArgs e) {
            BindData();
        }

        protected string GetBlogRssUrl() {
            return SEOHelper.GetBlogRssUrl();
        }

        private void BindData() {
            var blogEntries = _blogService.GetAllBlogPosts(NopContext.Current.WorkingLanguage.LanguageId, 0, _blogService.PostsPageSize);
            if (blogEntries != null && blogEntries.Count > 0) {
                rptHomeBlog.DataSource = blogEntries;
                rptHomeBlog.DataBind();
            }
            else {
                Visible = false;
            }
        }

        protected string GetBlogRSSUrl() {
            return SEOHelper.GetBlogRssUrl();
        }
    }
}